<?php

declare(strict_types=1);

namespace App\Command;

use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Psr\Container\ContainerInterface;
use Psr\SimpleCache\InvalidArgumentException;
use Symfony\Component\Console\Input\InputOption;

#[Command]
class TestCommand extends HyperfCommand
{
    public function __construct(protected ContainerInterface $container)
    {
        parent::__construct('test');
    }

    public function configure()
    {
        parent::configure();
        $this->setDescription('Test Command')
            ->addOption('method', null, InputOption::VALUE_OPTIONAL, 'exec method')
            ->addOption('args', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'exec method args');
    }

    public function handle()
    {
        $method = sprintf('custom%s', ucfirst((string)$this->input->getOption('method')));
        if (method_exists($this, $method)) {
            $args = $this->input->getOption('args');
            call_user_func([$this, $method], ...$args);
        }
        $this->line('Complete.', 'info');
    }

    /**
     * 生成令牌
     * @param string $scene
     * @param mixed $id
     * @return void
     * @throws InvalidArgumentException
     */
    private function customGenToken(string $scene, mixed $id): void
    {
        // 生成Token
        $token = user($scene)->getToken([
            'id' => $id,
        ]);
        $this->line(sprintf('token: %s', $token));
    }
}
